package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import com.AAL.DAO.Interface.IEmployeeDAO;
import com.AAL.Model.Department;
import com.AAL.Model.Employee;
import com.AAL.Utils.HibernateUtil;

public class EmployeeDAO implements IEmployeeDAO {

	@Override
	public Employee saveAndUpdateEmployee(Employee employee) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(employee.getAccount());
			session.saveOrUpdate(employee.getAccount().getAccountInformation());
			session.saveOrUpdate(employee);
		} catch (Exception e) {
			System.out.println("Loi: " + e.getMessage());			
			return null;
		}
		session.getTransaction().commit();
		return employee;
	}

	@Override
	public Employee deleteEmployee(int employeeID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Employee employee = (Employee) session.load(Employee.class,
					employeeID);
			if (null != employee) {
				session.delete(employee);
			}
			session.getTransaction().commit();
			return employee;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public Employee getEmployee(int employeeID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Employee employee = (Employee) session.load(Employee.class, employeeID);
		System.out.println("employee.getAccount().getUserName() la: "
				+ employee.getAccount().getUserName());
		return employee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> getAllListEmployee() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Employee> listEmployee = null;
		try {

			listEmployee = (List<Employee>) session
					.createQuery("from Employee").list();
			// System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listEmployee;
	}

	// Phatdt start
	// search
	@SuppressWarnings("unchecked")
	public List<Employee> getAllListEmployeeBySearch(String fullname,
			String depmtchose, String statusChose) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Employee> listEmployee = null;
		String SQL_QUERY = null;
		if (fullname.equals("")) {
			fullname = "%";
		}
		if (!depmtchose.equals("0") && !statusChose.equals("0")) {
			SQL_QUERY = "from Employee as e where e.account.accountInformation.fullName like:name and e.department.departmentID='"
					+ depmtchose
					+ "' and e.account.statusOfAccount.statusOfAccountID='"
					+ statusChose + "'";
		} else {
			if (depmtchose.equals("0") && statusChose.equals("0")) {
				SQL_QUERY = "from Employee as e where e.account.accountInformation.fullName like:name";
			} else {
				if (!depmtchose.equals("0") && statusChose.equals("0")) {
					SQL_QUERY = "from Employee as e where e.account.accountInformation.fullName like:name and e.department.departmentID='"
							+ depmtchose + "'";
				} else {
					SQL_QUERY = "from Employee as e where e.account.accountInformation.fullName like:name and e.account.statusOfAccount.statusOfAccountID='"
							+ statusChose + "'";
				}
			}
		}

		try {
			listEmployee = (List<Employee>) session.createQuery(SQL_QUERY)
					.setParameter("name", "%" + fullname + "%").list();
			// System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listEmployee;
	}

	// Phatdt search end
	@Override
	public Employee getLstEmployeeByDepartment(Department department) {
		// TODO Auto-generated method stub
		return null;
	}

}
